//
//  PhotoScrollView.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/23.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "PhotoScrollView.h"

@implementation PhotoScrollView

- (nonnull instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delegate = self;
        self.contentSize = self.bounds.size;
        self.maximumZoomScale = 3;
        self.minimumZoomScale = 0.5;
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)];
        doubleTap.numberOfTapsRequired = 2;
        [self addGestureRecognizer:doubleTap];
        
        UITapGestureRecognizer *singeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singeTap)];
        singeTap.numberOfTapsRequired = 1;
        [singeTap requireGestureRecognizerToFail:doubleTap];
        [self addGestureRecognizer:singeTap];
    }
    return self;
}

- (void)singeTap {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HiddenTabBarNotification" object:nil];
}

- (void)doubleTap {
    //self.zoomScale = self.zoomScale == 1 ? 2 : 1;
    if (self.zoomScale == 1) {
        [self setZoomScale:3 animated:YES];
    } else {
        [self setZoomScale:1 animated:YES];
    }
   
}


- (void)scrollViewDidZoom:(nonnull UIScrollView *)scrollView {
    [self reSetPosition];
}

- (void)reSetPosition {
    if (self.zoomScale < 1) {
        _imageView.center = self.center;
    }
}


- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    self.zoomScale = 1;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_imageUrl] placeholderImage:[UIImage imageNamed:@"1"]];
    [self setNeedsLayout];
}

- (nullable UIView *)viewForZoomingInScrollView:(nonnull UIScrollView *)scrollView {
    return _imageView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
